package com.bobo.web.provider.controller.api;

import com.bobo.web.api.domain.game.PlayPlanDO;
import com.bobo.web.provider.service.PlanPlayService;
import com.bobo.web.provider.util.JedisUtils;
import com.bobo.web.provider.util.RedisUtils;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.ExecutorService;

import static java.util.concurrent.Executors.newFixedThreadPool;

@RestController
@RequestMapping("api/game")
public class GameController {
    private static Logger LOGGER = LoggerFactory.getLogger(GameController.class.getSimpleName());
    private static String LOCK_KEY = "12345";

    @Resource
    private AsyncTest asyncTest;
    @Resource
    private PlanPlayService planPlayService;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private JedisUtils jedisUtils;

    /**
     * 创建篮球计划
     *
     * @param playPlanDO
     * @return
     */
    @RequestMapping("creatPlanPlay")
    public void creatPlanPlay(@RequestBody PlayPlanDO playPlanDO) {
        asyncTest.doTaskOne(playPlanDO);
    }

    /**
     * 测试锁可用性
     *
     * @param playPlanDO
     * @return
     */
    @RequestMapping("updatePlanPlay")
    public Long updatePlanPlay(@RequestBody PlayPlanDO playPlanDO) {
        ExecutorService executorService = newFixedThreadPool(100);
//        for (int i = 0; i < 100000; i++) {
//            executorService.submit(() -> {
//                String requestId = UUID.randomUUID().toString();
//                if (jedisUtils.tryGetDistributedLock("12345", requestId, 500L)) {
//
//                    try {
//                        LOGGER.info(String.format("请求ID=%s,加锁成功", requestId));
//                        Thread.currentThread().sleep(RandomUtils.nextInt(10000,550000));
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    } finally {
//                        boolean releaseLockFlag = jedisUtils.releaseDistributedLock(null, LOCK_KEY, requestId);
//                        LOGGER.info(String.format("请求ID=%s,释放锁结果,%s", requestId, releaseLockFlag));
//                    }
//                }
//            });
//        }
        for (int i = 0; i < 100000; i++) {
            new Thread(()->{
                String requestId = UUID.randomUUID().toString();
                if (jedisUtils.tryGetDistributedLock("12345", requestId, 500L)) {
                    try {
                        LOGGER.info(String.format("请求ID=%s,加锁成功", requestId));
                        Thread.currentThread().wait(RandomUtils.nextInt(10000,550000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        boolean releaseLockFlag = jedisUtils.releaseDistributedLock(null, LOCK_KEY, requestId);
                        LOGGER.info(String.format("请求ID=%s,释放锁结果,%s", requestId, releaseLockFlag));
                    }
                }
            }).start();
        }
        return 1L;
    }
}
